package cn.szzsi.service;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.core.kit.UtilDate;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.UserPointDetail;

import java.util.Date;
import java.util.List;

/**
 * 积分明细
 * Created by zhengy on 2017/2/27.
 */
public class UserPointDetailService {
    public static final UserPointDetailService service = new UserPointDetailService();

    /**
     *  保存积分明细
     * @param mid 会员id
     * @param addPoint 交易积分
     * @param type 积分类型
     * @param memo 备注
     */
    public void saveInfo(String mid, Integer addPoint, int type, String memo){
        UserPointDetail userPointDetail=new UserPointDetail();
        userPointDetail.set("id", CommonUtil.getUUID());
        userPointDetail.set("mid",mid);
        userPointDetail.set("addPoint",addPoint);
        userPointDetail.set("type",type);
        userPointDetail.set("memo",memo);
        userPointDetail.set("createDate",new Date());
        userPointDetail.save();
    }

    /**
     *  保存积分明细(会员签到)
     * @param mid 会员id
     * @param addPoint 交易积分
     * @param type 积分类型
     * @param memo 备注
     */
    public void saveSignInfo(String mid, Integer addPoint, int type, String memo,Integer continueSingDays){
        UserPointDetail userPointDetail=new UserPointDetail();
        userPointDetail.set("id", CommonUtil.getUUID());
        userPointDetail.set("mid",mid);
        userPointDetail.set("addPoint",addPoint);
        userPointDetail.set("type",type);
        userPointDetail.set("memo",memo);
        userPointDetail.set("continueSingDays",continueSingDays);
        userPointDetail.set("createDate",new Date());
        userPointDetail.save();
    }
    public void testSaveSignInfo(String mid, Integer addPoint, int type, String memo,Integer continueSingDays,Date date){
        UserPointDetail userPointDetail=new UserPointDetail();
        userPointDetail.set("id", CommonUtil.getUUID());
        userPointDetail.set("mid",mid);
        userPointDetail.set("addPoint",addPoint);
        userPointDetail.set("type",type);
        userPointDetail.set("memo",memo);
        userPointDetail.set("continueSingDays",continueSingDays);
        userPointDetail.set("createDate",date);
        userPointDetail.save();
    }

    /**
     * 判断会员是否是首次签到
     * @param memberId
     * @return
     */
    public boolean isFirstSign(String memberId){
        List<UserPointDetail> userSign= UserPointDetail.dao.find("select * from mall_user_point_detail where mid=? and type='0'",memberId);
        if(userSign.size()<1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 检查该会员今天是否签到
     * @param member
     * @return
     */
    public boolean checkIfUserSign(Member member){
        boolean flag = false;
        UserPointDetail lastSign= getLastSignRecord(member.getStr("id"));//查询买家最后一次签到记录
        if(lastSign!=null){
            Long lastDateTime=lastSign.getDate("createDate").getTime();//最后一次签到时间戳
            Long todatStartTime= UtilDate.getTodayStartTime();//当天凌晨时间戳
            if(todatStartTime<lastDateTime){//已经签到
                flag = true;
            }
        }
        return flag;
    }

    /**
     * //获取最后一次签到记录
     * @param memberId
     * @return
     */
    public UserPointDetail getLastSignRecord(String memberId){
        return UserPointDetail.dao.findFirst("select * from mall_user_point_detail where mid=? and type='0' order by createDate desc limit 1",memberId);
    }
}


















